﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bot.Application.Dto;
using Bot.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace Bot.WebAPI.Controllers
{
    [Route("api/v3/chatbot")]
    [ApiController]
    public class EntitiesController : ControllerBase
    {

        private readonly IEntityAppService _app;
        public EntitiesController(IEntityAppService app)
        {
            this._app = app;
        }

        [HttpGet]
        [Route("entities/{id:Guid}")]
        public ActionResult<EntityDto> Get([FromRoute] Guid id)
        {
            EntityDto entity = _app.Get(id);
            return Ok(entity);
        }

        [HttpGet]
        [Route("bots/{botId:Guid}/entities")]
        public ActionResult<IEnumerable<EntityDto>> GetList([FromRoute] Guid botId, [FromQuery] string include)
        {
            IEnumerable<EntityDto> list = _app.GetList(botId, include);
            return Ok(list);
        }

        [HttpPut]
        [Route("entities/{id:Guid}")]
        public ActionResult<EntityDto> Put([FromRoute] Guid id, [FromBody] EntityUpdateDto dto)
        {
            EntityDto entity = _app.Update(id, dto);
            return Ok(entity);
        }

        [HttpPost]
        [Route("bots/{botId:Guid}/entities")]
        public ActionResult<EntityDto> Post([FromRoute] Guid botId, [FromBody] EntityCreateDto dto)
        {
            EntityDto entity = _app.Add(botId, dto);
            return CreatedAtAction(nameof(Get), new { id = entity.Id }, entity);
        }

        [HttpDelete]
        [Route("entities/{id:Guid}")]
        public ActionResult Delete([FromRoute] Guid id)
        {
            _app.Delete(id);
            return NoContent();
        }

        [HttpPost]
        [Route("bots/{botId:Guid}/entities:import")]
        public ActionResult<OperationDto> Import([FromRoute] Guid botId)
        {
            string filePath = "";
            OperationDto operationDto = _app.Import(botId, filePath);
            return Ok(operationDto);
        }
    }
}
